<?php
/******************************************************
v4 - Management Framework
Copyright (C) 2008  Gary Taylor, gMerc Incorporation
http://www.gmerc.com/v4/

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

gary@gmerc.com
******************************************************/
class config_editor {
  function loaddata() {
    global $masterclass;

    $masterclass->loadjavascript('',$masterclass->mainconfig['v4rootdir'].'include/suckertreemenu.inc.js');
    
    if (is_file($masterclass->mainconfig['v4rootdir'].'include/defaultconfig.inc.php')) { $defaultfile=$masterclass->mainconfig['v4rootdir'].'include/defaultconfig.inc.php'; }
    else if (is_file('defaultconfig.inc.php.php')) { $defaultfile='defaultconfig.inc.php'; }
    else if (is_file('../defaultconfig.inc.php')) { $defaultfile='../defaultconfig.inc.php'; }
    else if (is_file('../include/defaultconfig.inc.php')) { $defaultfile='../include/defaultconfig.inc.php'; }

    if (is_file('userconfig.php')) { $userfile='userconfig.php'; }
    else if (is_file('../userconfig.php')) { $userfile='../userconfig.php'; }
    else if (is_file('../include/userconfig.php')) { $userfile='../include/userconfig.php'; }

    if ($defaultfile!='' && is_file($defaultfile)) {
      $tmparr=$masterclass->load_config($defaultfile);
      if (!empty($tmparr)) {
        foreach ($tmparr as $obj=>$val) { $masterclass->site['configeditor']->$obj=array();$masterclass->site['configeditor']->$obj=array_merge($masterclass->site['configeditor']->$obj,$tmparr->$obj); }
      }
    } else { echo "FATAL ERROR!!: Cannot Find Default Config File, Call Admin!";exit(); }
    if ($userfile!='' && is_file($userfile)) {
      $tmparr=$masterclass->load_config($userfile);
      if (!empty($tmparr)) {
        foreach ($tmparr as $obj=>$val) { $masterclass->site['configeditor']->$obj=array();$masterclass->site['configeditor']->$obj=array_merge($masterclass->site['configeditor']->$obj,$tmparr->$obj); }
      }
    }  else { echo "ERROR: Cannot Find Users Config File"; }
    

  return;
  }

  function savedata() {
    global $masterclass;
    
    $userdata='';
    foreach ($masterclass->site['configeditor'] as $obj=>$objdata) {
      foreach ($objdata as $key=>$val) {
          
        switch (gettype($val)) {
         default:
          $userdata.='$userconfig->'.$obj.'[\''.$key.'\']=\'';
          $userdata.=$masterclass->sitevars['configeditor_'.$obj.'_'.$key];
          $userdata.='\';';
          $userdata.="\n";
         break;
         case 'boolean':
         case 'integer':
         case 'double':
          $userdata.='$userconfig->'.$obj.'[\''.$key.'\']='.$masterclass->sitevars['configeditor_'.$obj.'_'.$key].';';
          $userdata.="\n";
         break;
         
        }
      }
    }
    if ($userdata!='') {
     if (is_file('userconfig.php')) { $userfile='userconfig.php'; }
      else if (is_file('../userconfig.php')) { $userfile='../userconfig.php'; }
      else if (is_file('../include/userconfig.php')) { $userfile='../include/userconfig.php'; }
      if ($userfile!='' && is_file($userfile)) {
        file_put_contents($userfile,"<?php\n".$userdata."\n?>");
      }  else { echo "ERROR: Cannot Find Users Config File"; }
    }
  }
  function display() {
    global $masterclass;


echo '<div class="sitebox_navigation" style="width:100%;">';
echo '<ul id="sitebox_navmenu">';
echo '<li OnClick="jsshowloadingscreen();add_post_action(\'customers_save\');document.getElementById(\'frm\').submit();"><a>Save</a></li>';
echo '<li OnClick="window.close();"><a>Close</a></li>';
echo '</ul>';
echo '<br style="clear: left;" />';
echo '</div>';


    echo "<table>";
    foreach ($masterclass->site['configeditor'] as $obj=>$objdata) {
      foreach ($objdata as $key=>$val) {
        switch ($obj.'->'.$key) {
         default:
          echo '<tr><td>'.$masterclass->allclasses['websites']->disp_inputtext_static('configeditor_'.$obj.'_'.$key,$val).'</td><td>'.$obj.'->'.$key.'</td></tr>';
         break;
         case 'site->defaultpermission':
         case 'site->defaultpagepermission':
         case 'site->defaultfieldpermission':
          $options=array();
          $options[]=array('allow');
          $options[]=array('deny');
          echo '<tr><td>'.$masterclass->allclasses['websites']->disp_select_static($options,'configeditor_'.$obj.'_'.$key,$val).'</td><td>'.$obj.'->'.$key.'</td></tr>';
         break;
         case 'websiteconfig->page_loading':
         case 'websiteconfig->page_processing':
          echo '<tr><td colspan="2">'.$obj.'->'.$key.'<br>'.$masterclass->allclasses['websites']->disp_textarea_static('configeditor_'.$obj.'_'.$key,$val).'</td></tr>';
         break;
        }
      }
    }
    echo "</table>";

  return;
  }
}
?>